package com.situ.student.service.impl;

import java.awt.Checkbox;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.situ.student.dao.IStudentDao;
import com.situ.student.dao.impl.StudentDaoJdbcImpl;
import com.situ.student.entity.Banji;
import com.situ.student.entity.PageBean;
import com.situ.student.entity.PageBean2;
import com.situ.student.entity.Student;
import com.situ.student.service.IStudentService;

public class StudentServiceImpl implements IStudentService {
	// private IStudentDao studentDao = new StudentDaoMemoryImpl();
	private IStudentDao studentDao = new StudentDaoJdbcImpl();

	@Override
	public boolean insert(Student student) {
		int count = studentDao.insert(student);
		return count == 1 ? true : false;
	}

	@Override
	public List<Student> list() {
		//拿到数据库中查找的数据
		List<Student> list = studentDao.list();
		//新建一个集合
		List<Student> copyList = new ArrayList<>();
		for (Student student : list) {
			student.setName("Java1807-" + student.getName());
			copyList.add(student);
		}
//		return copyList;
		return studentDao.list();
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = studentDao.deleteById(id);
		return count == 1 ? true : false;
	}

	@Override
	public Student findById(Integer id) {
		return studentDao.findById(id);
		
	}

	private List<Student> ListLike() {
		List<Student> list = studentDao.list();
		List<Student> copyList = new ArrayList<>();
		for (Student student : list) {
			student.setName(student.getName());
			copyList.add(student);
		}
		return copyList;

	}

	@Override
	public PageBean2 getPageBean(int pageNo, int pageSize) {
		PageBean2 pageBean2 = new PageBean2();
		// 当前是第几页
		pageBean2.setPageNo(pageNo);
		// 一页有多少数据
		pageBean2.setPageSize(pageSize);
		// 总记录数
		int totalCount = studentDao.getTotalCount();
		pageBean2.setTotalCount(totalCount);
		// 一共有多少页(向上取整，不满三条也算一页)
		int totalpage = (int) Math.ceil((double) totalCount / pageSize);
		pageBean2.setTotalPage(totalpage);
		// 当前页的数据
		int offset = (pageNo - 1) * pageSize;//(limit第一个数据)
		List<Map<String, Object>> list = studentDao.pageList(offset, pageSize);
		pageBean2.setList(list);
		
		return pageBean2;
	}

	@Override
	public boolean checkName(String name) {
		int count =studentDao.selectCountByName(name);
		
		return count>0?true:false;
	}

	@Override
	public boolean toupdate(Student student) {
		int count=studentDao.toupdate(student);//修改有问题
		return count==1?true:false;
	}
}
